/*
 * OmniLight.cpp
 *
 *  Created on: 26 mai 2011
 *      Author: seeme
 */
#include "OmniLight.h"

OmniLight::~OmniLight(){
	ResCache * s = Singleton<ResCache>::get();
	s->getResource("test.basicLight")->downRefCount();
	s->getResource("test.LightGyzmo")->downRefCount();
	delete m_gyzmo;
}

OmniLight::OmniLight(Vect4<float> c, Vect4<float> pos, unsigned int number=0): AbstractLight(c){
	//On crée un drawable entity pour afficher un gyzmo..
	ResCache * s = Singleton<ResCache>::get();

	//Son modele:

	//Le nom
	std::string name("LIGHT");
	std::stringstream st;
	st << number;
	name += st.str();

	std::vector<Point> points;
	points.push_back(Point(-5.f, -5.f, 0.0f, 0.f, 0.f));
	points.push_back(Point(5.f, -5.f, 0.0f, 1.f, 0.f));
	points.push_back(Point(5.f, 5.f, 0.0f, 1.f, 1.f));
	points.push_back(Point(-5.f, 5.f, 0.0f, 0.f, 1.f));

	std::vector<Triangle<unsigned int> > triangles;
	triangles.push_back(Triangle<unsigned>(2, 3, 0));
	triangles.push_back(Triangle<unsigned>(0, 1, 2));

	Material* material = new Material();

	//La texture
	std::string packDir = s->getPackageDir();
	std::string texPath = packDir+std::string("Textures/light.png");
	//Texture* tex = new Texture("test.basicLight", texPath);

	//Un shader par défaut.
	Shader* vs = (Shader*)s->getResource("test.shader.vLightGizmo");
	Shader* fs = (Shader*)s->getResource("test.shader.fLightGizmo");

	Program* program = new Program(vs->getShaderId(), fs->getShaderId());
	vs->downRefCount();
	fs->downRefCount();

	Mesh* m = new Mesh(name, points, triangles, material);
	m->setTexture((Texture*)s->getResource("test.lightGyzTex"));
	m->addProgram("AMBIANT", program);
	delete program;

	m_gyzmo = new StaticDrawableEntity(pos, Vect4<float>(1., 1., 1.), Vect4<float>(1., 1., 1.), m);
	//On enregistre tout ça pour que ce soit pris en compte par le gestionnaire de mémoire
	s->addResource("test.LightGyzmo", m);
	s->getResource("test.LightGyzmo");
}
